/*
 * Copyright 2012 Alain Pannetrat (L1L1@gmx.com)
 * 
 * This file is part of cookie-miner.
 * 
 * cookie-miner is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * cookie-miner is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with cookie-miner. If not, see http://www.gnu.org/licenses/.
 *
 * @version: $Id$
 *
 */
#ifndef TCP_H
#define TCP_H

int tcp_connect(const char *hoststring);

int tcp_listen(unsigned port);

int tcp_accept(int sock, char *caller);

void tcp_close(int sock);

class SocketAddress;

SocketAddress socket_get_local_address(int sock);

SocketAddress socket_get_peer_address(int sock);

class SocketAddress {
	public:
		SocketAddress() : _port(0) {
			_ip_addr[0]=0; }

		SocketAddress(const char *ip_addr, unsigned aport) : _port(0) {
			ip(ip_addr); port(aport); }

		SocketAddress(const char *ip_port) : _port(0) {
			set(ip_port); }
		
		SocketAddress(const SocketAddress& sa) : _port(0) {
			ip(sa.ip()); port(sa.port()); }
		
		SocketAddress& operator=(const SocketAddress& sa) {
			if (&sa!=this) { ip(sa.ip()); port(sa.port()); } return *this; }
				
		bool set(const char *ip_port);
		
		const char *ip() const { 
			return _ip_addr; }
		
		bool ip(const char *ip_addr);
		
		unsigned port() const { 
			return _port; }
		
		bool port(unsigned aport) {
			_port = aport; return true; }
	private:
		char _ip_addr[40];
		unsigned _port;
};

#endif
